<?php
namespace app\company\controller;

use OSS\OssClient;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Controller;

class File extends Admin
{
    public function uploadImg()
    {
        if(empty($_FILES['file'])){
            json_error_msg("请选择文件！");
        }
        if($_FILES['file']['error'] != 0) {
            if ($_FILES['file']['error'] == 1 || $_FILES['file']['error'] == 2) {
                json_error_msg("超过了系统设定的文件大小");
            } elseif ($_FILES['file']['error'] == 3) {
                json_error_msg(' 文件只有部分被上传');
            } elseif ($_FILES['file']['error'] == 4) {
                json_error_msg(' 没有文件被上传');
            } elseif ($_FILES['file']['error'] == 5) {
                json_error_msg(' 上传文件大小为0');
            } else {
                json_error_msg('文件上传失败');
            }
        }
        $dicname = input('dicName','projectfile','trim');
        //名字获取
        $file_name= date('YmdHis').rand(1,999).$_FILES['file']['name'];//拼装存储地址path
       $filekey = $this->uploadSysFileToOSS($_FILES['file']['tmp_name'],$file_name,$dicname);
       $res["file"] = "https://file.hot68.cn/htqpfile/".$dicname."/".$filekey;
        // $res["file"] = "https://file.hot68.cn/htqpfile/paypic/20241229210334287logo.png";
        $res["file_name"] = $file_name;
        json_success_msg($res);
    }

    private function uploadSysFileToOSS($filepath,$filename,$dicname){
        vendor('OSS.src.OSS.OssClient');
        vendor('OSS.Common');
        vendor('OSS.Config');
        $config = array(
            'KeyId' => 'LTAI4GH2fKDxQGVheJZXKnd7',
            'KeySecret' => 'aP2lIjWuspC5D6E1ATgXezESgsuHF0',
            'Endpoint' => 'oss-cn-beijing.aliyuncs.com',
            'Bucket' => 'gangboshi',
        );
        $ossClient = new OssClient($config['KeyId'], $config['KeySecret'],$config['Endpoint'], false);
        if (is_null($ossClient)) json_error_msg('上传失败');
        //*******************************简单使用***************************************************************
        //上传本地文件
        try{
            $result = $ossClient->uploadFile($config['Bucket'],'htqpfile/'.$dicname.'/'.$filename,$filepath);
        }catch (\Exception $e){
            json_error_msg($e->getMessage());
        }
        return $filename;
    }

}

